<template>
  <div class="dialog">
    <el-dialog :title="titleText + '物料类型'" :visible.sync="showDialog" width="30%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px"
        class="demo-ruleForm">

        <el-form-item label="物料类型:" prop="materialTypeName">
          <el-input v-model="formData.materialTypeName" clearable placeholder="请输入物料类型名字" />
        </el-form-item>

        <el-form-item label="物料类型编码:" prop="materialTypeNo">
          <el-input :disabled="nowChanceStau=='edit'" v-model="formData.materialTypeNo" clearable placeholder="请输入物料类型编码" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import MaterialTypeApi from "@/api/basic-info-manage/material-type-manage/index"
import EnumApi from "@/api/enum"
export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {},
      rules: {
        materialTypeName: [
          { required: true, message: "请输入物料类型名字", trigger: ["change", "blur"] }
        ],
        materialTypeNo: [
          { required: true, message: "请输入物料类型编码", trigger: ["change", "blur"] }
        ],
      },
      materialTypeName: [
        {
          typeName: "饲料",
          typeCode: "FEED",
        },
        {
          typeName: "药品",
          typeCode: "DRUGS",
        },
        {
          typeName: "物料",
          typeCode: "MATERIAL",
        },
      ],
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created() {
  },
  mounted() {
    // this.queryEnum()
  },
  methods: {
    // queryEnum(){
    //   EnumApi.list({enumType:'MaterialType'}).then(res=>{
    //     this.materialType = res.data
    //   })
    // },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            MaterialTypeApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            MaterialTypeApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
